package com.etang.a2020d1021.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.etang.a2020d1021.R;
import com.etang.a2020d1021.tools.DiyToast;

public class ControlFragment extends Fragment {

    private ToggleButton tg_lamp_1, tg_lamp_2, tg_warm, tg_door, tg_fan, tg_kt, tg_tv, tg_dvd;
    private ImageView iv_cur_open, iv_cur_stop, iv_cur_cls;
    private ImageView iv_lamp_1, iv_lamp_2, iv_warm, iv_door, iv_fan, iv_kt, iv_tv, iv_dvd;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_control, null, false);
        initView(view);
        iv_cur_cls.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiyToast.showToast(getActivity(), "窗帘关");
                ControlUtils.control(ConstantUtil.Curtain, ConstantUtil.CHANNEL_2, ConstantUtil.OPEN);
            }
        });
        iv_cur_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiyToast.showToast(getActivity(), "窗帘开");
                ControlUtils.control(ConstantUtil.Curtain, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
            }
        });
        iv_cur_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DiyToast.showToast(getActivity(), "窗帘停");
                ControlUtils.control(ConstantUtil.Curtain, ConstantUtil.CHANNEL_3, ConstantUtil.OPEN);
            }
        });
        tg_warm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_warm.setBackgroundResource(R.drawable.ic_warm_on);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    iv_warm.setBackgroundResource(R.drawable.ic_warm_off);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_lamp_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_lamp_1.setBackgroundResource(R.drawable.ic_lamp_on);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
                } else {
                    iv_lamp_1.setBackgroundResource(R.drawable.ic_lamp_off);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_1, ConstantUtil.CLOSE);
                }
            }
        });
        tg_lamp_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_lamp_2.setBackgroundResource(R.drawable.ic_lamp_on);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_2, ConstantUtil.OPEN);
                } else {
                    iv_lamp_2.setBackgroundResource(R.drawable.ic_lamp_off);
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_2, ConstantUtil.CLOSE);
                }
            }
        });
        tg_tv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_tv.setBackgroundResource(R.drawable.ic_tv_on);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "1", ConstantUtil.OPEN);
                } else {
                    iv_tv.setBackgroundResource(R.drawable.ic_tv_off);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "1", ConstantUtil.OPEN);
                }
            }
        });
        tg_kt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_kt.setBackgroundResource(R.drawable.ic_kt_on);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "2", ConstantUtil.OPEN);
                } else {
                    iv_kt.setBackgroundResource(R.drawable.ic_kt_off);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "2", ConstantUtil.OPEN);
                }
            }
        });
        tg_fan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_fan.setBackgroundResource(R.drawable.ic_fan_on);
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    iv_fan.setBackgroundResource(R.drawable.ic_fan_off);
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_dvd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    iv_dvd.setBackgroundResource(R.drawable.ic_dvd_on);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "3", ConstantUtil.OPEN);
                } else {
                    iv_dvd.setBackgroundResource(R.drawable.ic_dvd_off);
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "3", ConstantUtil.OPEN);
                }
            }
        });
        return view;
    }

    private void initView(View view) {
        tg_door = (ToggleButton) view.findViewById(R.id.tg_door);
        tg_dvd = (ToggleButton) view.findViewById(R.id.tg_dvd);
        tg_fan = (ToggleButton) view.findViewById(R.id.tg_fan);
        tg_kt = (ToggleButton) view.findViewById(R.id.tg_kt);
        tg_lamp_1 = (ToggleButton) view.findViewById(R.id.tg_lamp_1);
        tg_lamp_2 = (ToggleButton) view.findViewById(R.id.tg_lamp_2);
        tg_tv = (ToggleButton) view.findViewById(R.id.tg_tv);
        tg_warm = (ToggleButton) view.findViewById(R.id.tg_warm);
        iv_cur_cls = (ImageView) view.findViewById(R.id.iv_cur_close);
        iv_cur_open = (ImageView) view.findViewById(R.id.iv_cur_open);
        iv_cur_stop = (ImageView) view.findViewById(R.id.iv_cur_stop);
        iv_door = (ImageView) view.findViewById(R.id.iv_door);
        iv_dvd = (ImageView) view.findViewById(R.id.iv_dvd);
        iv_fan = (ImageView) view.findViewById(R.id.iv_fan);
        iv_kt = (ImageView) view.findViewById(R.id.iv_kt);
        iv_lamp_1 = (ImageView) view.findViewById(R.id.iv_lamp_1);
        iv_lamp_2 = (ImageView) view.findViewById(R.id.iv_lamp_2);
        iv_tv = (ImageView) view.findViewById(R.id.iv_tv);
        iv_warm = (ImageView) view.findViewById(R.id.iv_wamp);
    }
}
